from flask import Flask,request,render_template,jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate

app = Flask(__name__)




    # 配置数据库
HOSTNAME = "localhost"  # sql主机名或IP地址
PORT = 3306  # sql端口
USERNAME = "root"  # 用户名
PASSWORD = "root"  # 密码
DATABASE = "fwj"  # 数据库名称
app.config['SQLALCHEMY_DATABASE_URI'] = f"mysql+pymysql://{USERNAME}:{PASSWORD}@{HOSTNAME}:{PORT}/{DATABASE}?charset=utf8mb4"  

db = SQLAlchemy(app)
class User(db.Model):
    # 定义表名
    __tablename__ = 'users'
    # 定义字段
    id = db.Column(db.Integer, primary_key=True,autoincrement=True)
    username = db.Column(db.String(64), unique=True, index=True)
    password = db.Column(db.String(64))
migrate = Migrate(app, db)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/login')
def login():
    return render_template('login.html')

@app.route('/login', methods=['POST'])
def login1():
    username = request.json['username']
    password = request.json['password']
    print(username,password)
    user = User.query.filter_by(username=username,password=password).first()
    if user:
        print("true")
        return jsonify({'message': '登录成功'}), 200
    else:
        print("no") 
        return jsonify({'message': '用户不存在'}), 404


@app.route('/PersonalCenter')
def PersonalCenter():
    return render_template('PersonalCenter.html')

@app.route('/kind')
def kind():
    return render_template('kind.html')

@app.route('/adopt')
def adopt():
    return render_template('adopt.html')

@app.route('/Adoption')
def Adoption():
    return render_template('Adoption.html')

@app.route('/Blogger')
def Blogger():
    return render_template('Blogger.html')


@app.route('/register',methods = ['get','post'])
def register():
    if request.method == 'GET':
        return render_template('register.html')
    if request.method == 'POST':
        username = request.json['username']
        password = request.json['password']
        user = User()
        user.username=username
        user.password=password
        db.session.add(user)  
        db.session.commit() 
        return jsonify({'message': '注册成功'}), 200

@app.route('/change_password',methods = ['get','post'])
def change_password():
    if request.method == 'GET':
        return render_template('change_password.html')
    if request.method == 'POST':
        return jsonify({'message': '不许改，这是我的！！！！'}), 200
    


if __name__ == '__main__':

    app.run(debug=True,host='0.0.0.0',port=80)
